package com.app.controller;

import com.alibaba.fastjson.JSONArray;
import com.app.model.PersonModel;
import com.app.service.PersonService;
import com.app.util.ExcelUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

@RestController
@Controller
@RequestMapping("/api/excel")
public class ExcelController {

    @Autowired
    PersonService personService;
    @PostMapping("/import")
    public JSONArray importUser(@RequestPart("file") MultipartFile file) throws Exception {
        JSONArray array = ExcelUtils.readMultipartFile(file);
        System.out.println("导入数据为:" + array);
        return array;
    }

    @GetMapping("/export")
    public void export(HttpServletResponse response) throws IOException {
         List<PersonModel> personModelList = personService.listExport();
        formGeneration(response,personModelList);
    }

    public void formGeneration(HttpServletResponse response,List<PersonModel> modelList) {
        //查询数据库中的数据
        String[] AColumns = {"姓名","民族","入党时间","学历","人员类型","衔级","联系电话","详细住址","现任单位及职务"};
        String[] CColumns = {"性别","籍贯0","参加工作时间","所学专业","入伍时间","专业技术职务","身份证号"};
        String[] EColumns = {"出生日期","出生地","健康状况","毕业院校","岗位层级","是否特殊人才"};
        XSSFWorkbook xssfSheets = new XSSFWorkbook();

        XSSFSheet userList = xssfSheets.createSheet("基本信息");

        Row r1 = userList.createRow(0);//创建第一行，起始为0
        //单元格样式(表头)
        CellStyle headerStyle = xssfSheets.createCellStyle();

        //字体+文字大小(表头)
        Font headerFont = xssfSheets.createFont();
        headerFont.setFontName("宋体");
        headerFont.setFontHeightInPoints((short) 22);
        headerFont.setBold(true);
        headerFont.setColor(Font.COLOR_NORMAL);
        headerStyle.setFont(headerFont);
        // 左右居中
        headerStyle.setAlignment(HorizontalAlignment.CENTER);
        // 上下居中
        headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        headerStyle.setLocked(true);
        for (int i = 0; i < 8; i++) {
            userList.setColumnWidth(i, 6000);
        }
        //合并单元格7个单位（上下左右）
        userList.addMergedRegion(new CellRangeAddress(0, 0, 0, 7));
        r1.setHeight((short) 1000);
        Cell r10 = r1.createCell(0);
        r10.setCellValue("城市对象管理情况统计表");
        r10.setCellStyle(headerStyle);
        //内容样式
        CellStyle headerStyle2 = xssfSheets.createCellStyle();
        Font headerFont2 = xssfSheets.createFont();
        headerFont2.setFontName("宋体");
        headerFont2.setFontHeightInPoints((short) 11);
        headerFont2.setBold(true);
        headerFont2.setColor(Font.COLOR_NORMAL);
        headerStyle2.setFont(headerFont2);
        // 左右居中
        headerStyle2.setAlignment(HorizontalAlignment.CENTER);
        // 上下居中
        headerStyle2.setVerticalAlignment(VerticalAlignment.CENTER);
        headerStyle2.setLocked(true);
        //高
        Row r2 = userList.createRow(1);//创建第一行，起始为0
        r2.setHeight((short) 1000);
        Cell r20 = r2.createCell(0);
        r20.setCellValue("姓名");
        r20.setCellStyle(headerStyle2);

        Row r3 = userList.createRow(2);//创建第一行，起始为0
        r3.setHeight((short) 1000);
        Cell r30 = r3.createCell(0);
        r30.setCellValue("民族");
        r30.setCellStyle(headerStyle2);

        Row r4 = userList.createRow(3);//创建第一行，起始为0
        r4.setHeight((short) 1000);
        Cell r40 = r4.createCell(0);
        r40.setCellValue("入党时间");
        r40.setCellStyle(headerStyle2);


        String fileName = "人员.xlsx";
        OutputStream outputStream = null;
        try {
            fileName = URLEncoder.encode(fileName, "UTF-8");
            //设置ContentType请求信息格式
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName);

            outputStream = response.getOutputStream();
            xssfSheets.write(outputStream);
            outputStream.flush();
            outputStream.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
